public class Solution1 {
    public int[] dx = {0, 0, -1, 1};
    public int[] dy = {1, -1, 0, 0};
    public int color;
    public int m;
    public int n;
    public int target;

    public int[][] floodFill(int[][] image, int sr, int sc, int _color) {
        color = _color;
        m = image.length;
        n = image[0].length;
        target = image[sr][sc];
        if(target == color) {
            return image;
        }

        image[sr][sc] = color;
        dfs(image, sr, sc);
        return image;
    }

    public void dfs(int[][] image, int sr, int sc) {
        for(int i = 0; i< 4; i++) {
            int x = sr + dx[i];
            int y = sc + dy[i];
            if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == target) {
                image[x][y] = color;
                dfs(image, x, y);
            }
        }
    }
}
